<?php

namespace addons\oil\listener\order;

use addons\oil\exception\Exception;
use addons\sum\model\User;
use think\Db;

/**
 * 订单失效
 */
class Invalid
{
    // 订单取消后行为
    public function orderCancelAfter(&$params)
    {
        $order = $params['order'];

        $this->invalid($order, 'cancel');

        return $order;
    }


    // 订单关闭后行为
    public function orderCloseAfter(&$params)
    {
        $order = $params['order'];

        $this->invalid($order, 'close');
    }

    // 订单取消或关闭返还
    private function invalid($order, $type) {

        // 取消订单返还余额支付部分

        if ($order->money_fee > 0) {
            // 退回余额
            User::money($order->money_fee, $order->user_id, 'money_back_order', $order->id, '', [
                'order_id' => $order->id,
                'order_sn' => $order->order_sn,
            ]);
        }



        return $order;

    }
}
